home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Gtk2 / SimpleList.pm < prev    next >
Encoding:
Perl POD Document  |  2006-10-03  |  24.0 KB  |  843 lines

  1. #
  2. # $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gtk2/pm/SimpleList.pm,v 1.29 2006/10/03 15:49:15 kaffeetisch Exp $
  3. #
  4.  
  5. #########################
  6. package Gtk2::SimpleList;
  7.  
  8. use strict;
  9. use Carp;
  10. use Gtk2;
  11.  
  12. our @ISA = 'Gtk2::TreeView';
  13.  
  14. our $VERSION = '0.15';
  15.  
  16. our %column_types = (
  17.   'hidden' => {type=>'Glib::String',                                        attr=>'hidden'},
  18.   'text'   => {type=>'Glib::String',  renderer=>'Gtk2::CellRendererText',   attr=>'text'},
  19.   'markup' => {type=>'Glib::String',  renderer=>'Gtk2::CellRendererText',   attr=>'markup'},
  20.   'int'    => {type=>'Glib::Int',     renderer=>'Gtk2::CellRendererText',   attr=>'text'},
  21.   'double' => {type=>'Glib::Double',  renderer=>'Gtk2::CellRendererText',   attr=>'text'},
  22.   'bool'   => {type=>'Glib::Boolean', renderer=>'Gtk2::CellRendererToggle', attr=>'active'},
  23.   'scalar' => {type=>'Glib::Scalar',  renderer=>'Gtk2::CellRendererText',   
  24.       attr=> sub { 
  25.           my ($tree_column, $cell, $model, $iter, $i) = @_;
  26.           my ($info) = $model->get ($iter, $i);
  27.           $cell->set (text => $info || '' );
  28.       } },
  29.   'pixbuf' => {type=>'Gtk2::Gdk::Pixbuf', renderer=>'Gtk2::CellRendererPixbuf', attr=>'pixbuf'},
  30. );
  31.  
  32. # this is some cool shit
  33. sub add_column_type
  34. {
  35.     shift;    # don't want/need classname
  36.     my $name = shift;
  37.     $column_types{$name} = { @_ };
  38. }
  39.  
  40. sub text_cell_edited {
  41.     my ($cell_renderer, $text_path, $new_text, $slist) = @_;
  42.     my $path = Gtk2::TreePath->new_from_string ($text_path);
  43.     my $model = $slist->get_model;
  44.     my $iter = $model->get_iter ($path);
  45.     $model->set ($iter, $cell_renderer->{column}, $new_text);
  46. }
  47.  
  48. sub new {
  49.     croak "Usage: $_[0]\->new (title => type, ...)\n"
  50.         . " expecting a list of column title and type name pairs.\n"
  51.         . " can't create a SimpleList with no columns"
  52.         unless @_ >= 3; # class, key1, val1
  53.     return shift->new_from_treeview (Gtk2::TreeView->new (), @_);
  54. }
  55.  
  56. sub new_from_treeview {
  57.     my $class = shift;
  58.     my $view = shift;
  59.     croak "treeview is not a Gtk2::TreeView"
  60.         unless defined ($view)
  61.            and UNIVERSAL::isa ($view, 'Gtk2::TreeView');
  62.     croak "Usage: $class\->new_from_treeview (treeview, title => type, ...)\n"
  63.         . " expecting a treeview reference and list of column title and type name pairs.\n"
  64.         . " can't create a SimpleList with no columns"
  65.         unless @_ >= 2; # key1, val1
  66.     my @column_info = ();
  67.     for (my $i = 0; $i < @_ ; $i+=2) {
  68.         my $typekey = $_[$i+1];
  69.         croak "expecting pairs of title=>type"
  70.             unless $typekey;
  71.         croak "unknown column type $typekey, use one of "
  72.             . join(", ", keys %column_types)
  73.             unless exists $column_types{$typekey};
  74.         my $type = $column_types{$typekey}{type};
  75.         if (not defined $type) {
  76.             $type = 'Glib::String';
  77.             carp "column type $typekey has no type field; did you"
  78.                . " create a custom column type incorrectly?\n"
  79.                . "limping along with $type";
  80.         }
  81.         push @column_info, {
  82.             title => $_[$i],
  83.             type => $type,
  84.             rtype => $column_types{$_[$i+1]}{renderer},
  85.             attr => $column_types{$_[$i+1]}{attr},
  86.         };
  87.     }
  88.     my $model = Gtk2::ListStore->new (map { $_->{type} } @column_info);
  89.     # just in case, 'cause i'm paranoid like that.
  90.     map { $view->remove_column ($_) } $view->get_columns;
  91.     $view->set_model ($model);
  92.     for (my $i = 0; $i < @column_info ; $i++) {
  93.         if( 'CODE' eq ref $column_info[$i]{attr} )
  94.         {
  95.             $view->insert_column_with_data_func (-1,
  96.                 $column_info[$i]{title},
  97.                 $column_info[$i]{rtype}->new,
  98.                 $column_info[$i]{attr}, $i);
  99.         }
  100.         elsif ('hidden' eq $column_info[$i]{attr})
  101.         {
  102.             # skip hidden column
  103.         }
  104.         else
  105.         {
  106.             my $column = Gtk2::TreeViewColumn->new_with_attributes (
  107.                 $column_info[$i]{title},
  108.                 $column_info[$i]{rtype}->new,
  109.                 $column_info[$i]{attr} => $i,
  110.             );
  111.             $view->append_column ($column);
  112.     
  113.             if ($column_info[$i]{attr} eq 'active') {
  114.                 # make boolean columns respond to editing.
  115.                 my $r = $column->get_cell_renderers;
  116.                 $r->set (activatable => 1);
  117.                 $r->{column_index} = $i;
  118.                 $r->signal_connect (toggled => sub {
  119.                     my ($renderer, $row, $slist) = @_;
  120.                     my $col = $renderer->{column_index};
  121.                     my $model = $slist->get_model;
  122.                     my $iter = $model->iter_nth_child (undef, $row);
  123.                     my $val = $model->get ($iter, $col);
  124.                     $model->set ($iter, $col, !$val);
  125.                     }, $view);
  126.  
  127.             } elsif ($column_info[$i]{attr} eq 'text') {
  128.                 # attach a decent 'edited' callback to any
  129.                 # columns using a text renderer.  we do NOT
  130.                 # turn on editing by default.
  131.                 my $r = $column->get_cell_renderers;
  132.                 $r->{column} = $i;
  133.                 $r->signal_connect
  134.                     (edited => \&text_cell_edited, $view);
  135.             }
  136.         }
  137.     }
  138.  
  139.     my @a;
  140.     tie @a, 'Gtk2::SimpleList::TiedList', $model;
  141.  
  142.     $view->{data} = \@a;
  143.     return bless $view, $class;
  144. }
  145.  
  146. sub set_column_editable {
  147.     my ($self, $index, $editable) = @_;
  148.     my $column = $self->get_column ($index);
  149.     croak "invalid column index $index"
  150.         unless defined $column;
  151.     my $cell_renderer = $column->get_cell_renderers;
  152.     $cell_renderer->set (editable => $editable);
  153. }
  154.  
  155. sub get_column_editable {
  156.     my ($self, $index, $editable) = @_;
  157.     my $column = $self->get_column ($index);
  158.     croak "invalid column index $index"
  159.         unless defined $column;
  160.     my $cell_renderer = $column->get_cell_renderers;
  161.     return $cell_renderer->get ('editable');
  162. }
  163.  
  164. sub get_selected_indices {
  165.     my $self = shift;
  166.     my $selection = $self->get_selection;
  167.     return () unless $selection;
  168.     # warning: this assumes that the TreeModel is actually a ListStore.
  169.     # if the model is a TreeStore, get_indices will return more than one
  170.     # index, which tells you how to get all the way down into the tree,
  171.     # but all the indices will be squashed into one array... so, ah,
  172.     # don't use this for TreeStores!
  173.     map { $_->get_indices } $selection->get_selected_rows;
  174. }
  175.  
  176. sub select {
  177.     my $self = shift;
  178.     my $selection = $self->get_selection;
  179.     my @inds = (@_ > 1 && $selection->get_mode ne 'multiple')
  180.              ? $_[0]
  181.          : @_;
  182.     my $model = $self->get_model;
  183.     foreach my $i (@inds) {
  184.         my $iter = $model->iter_nth_child (undef, $i);
  185.         next unless $iter;
  186.         $selection->select_iter ($iter);
  187.     }
  188. }
  189.  
  190. sub unselect {
  191.     my $self = shift;
  192.     my $selection = $self->get_selection;
  193.     my @inds = (@_ > 1 && $selection->get_mode ne 'multiple')
  194.              ? $_[0]
  195.          : @_;
  196.     my $model = $self->get_model;
  197.     foreach my $i (@inds) {
  198.         my $iter = $model->iter_nth_child (undef, $i);
  199.         next unless $iter;
  200.         $selection->unselect_iter ($iter);
  201.     }
  202. }
  203.  
  204. sub set_data_array
  205. {
  206.     @{$_[0]->{data}} = @{$_[1]};
  207. }
  208.  
  209. sub get_row_data_from_path
  210. {
  211.     my ($self, $path) = @_;
  212.  
  213.     # $path->get_depth always 1 for SimpleList
  214.     # my $depth = $path->get_depth;
  215.  
  216.     # array has only one member for SimpleList
  217.     my @indices = $path->get_indices;
  218.     my $index = $indices[0];
  219.  
  220.     return $self->{data}->[$index];
  221. }
  222.  
  223. ##################################
  224. package Gtk2::SimpleList::TiedRow;
  225.  
  226. use strict;
  227. use Gtk2;
  228. use Carp;
  229.  
  230. # TiedRow is the lowest-level tie, allowing you to treat a row as an array
  231. # of column data.
  232.  
  233. sub TIEARRAY {
  234.     my $class = shift;
  235.     my $model = shift;
  236.     my $iter = shift;
  237.  
  238.     croak "usage tie (\@ary, 'class', model, iter)"
  239.         unless $model && UNIVERSAL::isa ($model, 'Gtk2::TreeModel');
  240.  
  241.     return bless {
  242.         model => $model,
  243.         iter => $iter,
  244.     }, $class;
  245. }
  246.  
  247. sub FETCH { # this, index
  248.     return $_[0]->{model}->get ($_[0]->{iter}, $_[1]);
  249. }
  250.  
  251. sub STORE { # this, index, value
  252.     return $_[0]->{model}->set ($_[0]->{iter}, $_[1], $_[2])
  253.         if defined $_[2]; # allow 0, but not undef
  254. }
  255.  
  256. sub FETCHSIZE { # this
  257.     return $_[0]{model}->get_n_columns;
  258. }
  259.  
  260. sub EXISTS { 
  261.     return( $_[1] < $_[0]{model}->get_n_columns );
  262. }
  263.  
  264. sub EXTEND { } # can't change the length, ignore
  265. sub CLEAR { } # can't change the length, ignore
  266.  
  267. sub new {
  268.     my ($class, $model, $iter) = @_;
  269.     my @a;
  270.     tie @a, __PACKAGE__, $model, $iter;
  271.     return \@a;
  272. }
  273.  
  274. sub POP { croak "pop called on a TiedRow, but you can't change its size"; }
  275. sub PUSH { croak "push called on a TiedRow, but you can't change its size"; }
  276. sub SHIFT { croak "shift called on a TiedRow, but you can't change its size"; }
  277. sub UNSHIFT { croak "unshift called on a TiedRow, but you can't change its size"; }
  278. sub SPLICE { croak "splice called on a TiedRow, but you can't change its size"; }
  279. #sub DELETE { croak "delete called on a TiedRow, but you can't change its size"; }
  280. sub STORESIZE { carp "STORESIZE operation not supported"; }
  281.  
  282.  
  283. ###################################
  284. package Gtk2::SimpleList::TiedList;
  285.  
  286. use strict;
  287. use Gtk2;
  288. use Carp;
  289.  
  290. # TiedList is an array in which each element is a row in the liststore.
  291.  
  292. sub TIEARRAY {
  293.     my $class = shift;
  294.     my $model = shift;
  295.  
  296.     croak "usage tie (\@ary, 'class', model)"
  297.         unless $model && UNIVERSAL::isa ($model, 'Gtk2::TreeModel');
  298.  
  299.     return bless {
  300.         model => $model,
  301.     }, $class;
  302. }
  303.  
  304. sub FETCH { # this, index
  305.     my $iter = $_[0]->{model}->iter_nth_child (undef, $_[1]);
  306.     return undef unless defined $iter;
  307.     my @row;
  308.     tie @row, 'Gtk2::SimpleList::TiedRow', $_[0]->{model}, $iter;
  309.     return \@row;
  310. }
  311.  
  312. sub STORE { # this, index, value
  313.     my $iter = $_[0]->{model}->iter_nth_child (undef, $_[1]);
  314.     $iter = $_[0]->{model}->insert ($_[1])
  315.         if not defined $iter;
  316.     my @row;
  317.     tie @row, 'Gtk2::SimpleList::TiedRow', $_[0]->{model}, $iter;
  318.     if ('ARRAY' eq ref $_[2]) {
  319.         @row = @{$_[2]};
  320.     } else {
  321.         $row[0] = $_[2];
  322.     }
  323.  
  324.     return $_[2];
  325. }
  326.  
  327. sub FETCHSIZE { # this
  328.     return $_[0]->{model}->iter_n_children (undef);
  329. }
  330.  
  331. sub PUSH { # this, list
  332.     my $model = shift()->{model};
  333.     my $iter;
  334.     foreach (@_)
  335.     {
  336.         $iter = $model->append;
  337.         my @row;
  338.         tie @row, 'Gtk2::SimpleList::TiedRow', $model, $iter;
  339.         if ('ARRAY' eq ref $_) {
  340.             @row = @$_;
  341.         } else {
  342.             $row[0] = $_;
  343.         }
  344.     }
  345.     return $model->iter_n_children (undef);
  346. }
  347.  
  348. sub POP { # this
  349.     my $model = $_[0]->{model};
  350.     my $index = $model->iter_n_children-1;
  351.     my $iter = $model->iter_nth_child(undef, $index);
  352.     return undef unless $iter;
  353.     my $ret = [ $model->get ($iter) ];
  354.     $model->remove($iter) if( $index >= 0 );
  355.     return $ret;
  356. }
  357.  
  358. sub SHIFT { # this
  359.     my $model = $_[0]->{model};
  360.     my $iter = $model->iter_nth_child(undef, 0);
  361.     return undef unless $iter;
  362.     my $ret = [ $model->get ($iter) ];
  363.     $model->remove($iter) if( $model->iter_n_children );
  364.     return $ret;
  365. }
  366.  
  367. sub UNSHIFT { # this, list
  368.     my $model = shift()->{model};
  369.     my $iter;
  370.     foreach (@_)
  371.     {
  372.         $iter = $model->prepend;
  373.         my @row;
  374.         tie @row, 'Gtk2::SimpleList::TiedRow', $model, $iter;
  375.         if ('ARRAY' eq ref $_) {
  376.             @row = @$_;
  377.         } else {
  378.             $row[0] = $_;
  379.         }
  380.     }
  381.     return $model->iter_n_children (undef);
  382. }
  383.  
  384. # note: really, arrays aren't supposed to support the delete operator this
  385. #       way, but we don't want to break existing code.
  386. sub DELETE { # this, key
  387.     my $model = $_[0]->{model};
  388.     my $ret;
  389.     if ($_[1] < $model->iter_n_children (undef)) {
  390.         my $iter = $model->iter_nth_child (undef, $_[1]);
  391.         return undef unless $iter;
  392.         $ret = [ $model->get ($iter) ];
  393.         $model->remove ($iter);
  394.     }
  395.     return $ret;
  396. }
  397.  
  398. sub CLEAR { # this
  399.     $_[0]->{model}->clear;
  400. }
  401.  
  402. # note: arrays aren't supposed to support exists, either.
  403. sub EXISTS { # this, key
  404.     return( $_[1] < $_[0]->{model}->iter_n_children );
  405. }
  406.  
  407. # we can't really, reasonably, extend the tree store in one go, it will be 
  408. # extend as items are added
  409. sub EXTEND {}
  410.  
  411. sub get_model {
  412.     return $_[0]{model};
  413. }
  414.  
  415. sub STORESIZE { carp "STORESIZE: operation not supported"; }
  416.  
  417. sub SPLICE { # this, offset, length, list
  418.     my $self = shift;
  419.     # get the model and the number of rows    
  420.     my $model = $self->{model};
  421.     # get the offset
  422.     my $offset = shift || 0;
  423.     # if offset is neg, invert it
  424.     $offset = $model->iter_n_children (undef) + $offset if ($offset < 0);
  425.     # get the number of elements to remove
  426.     my $length = shift;
  427.     # if len was undef, not just false, calculate it
  428.     $length = $self->FETCHSIZE() - $offset unless (defined ($length));
  429.     # get any elements we need to insert into their place
  430.     my @list = @_;
  431.     
  432.     # place to store any returns
  433.     my @ret = ();
  434.  
  435.     # remove the desired elements
  436.     my $ret;
  437.     for (my $i = $offset; $i < $offset+$length; $i++)
  438.     {
  439.         # things will be shifting forward, so always delete at offset
  440.         $ret = $self->DELETE ($offset);
  441.         push @ret, $ret if defined $ret;
  442.     }
  443.  
  444.     # insert the passed list at offset in reverse order, so the will
  445.     # be in the correct order
  446.     foreach (reverse @list)
  447.     {
  448.         # insert a new row
  449.         $model->insert ($offset);
  450.         # and put the data in it
  451.         $self->STORE ($offset, $_);
  452.     }
  453.     
  454.     # return deleted rows in array context, the last row otherwise
  455.     # if nothing deleted return empty
  456.     return (@ret ? (wantarray ? @ret : $ret[-1]) : ());
  457. }
  458.  
  459. 1;
  460.  
  461. __END__
  462. # documentation is a good thing.
  463.  
  464. =head1 NAME
  465.  
  466. Gtk2::SimpleList - A simple interface to Gtk2's complex MVC list widget
  467.  
  468. =head1 SYNOPSIS
  469.  
  470.   use Glib qw(TRUE FALSE);
  471.   use Gtk2 '-init';
  472.   use Gtk2::SimpleList;
  473.  
  474.   my $slist = Gtk2::SimpleList->new (
  475.                 'Text Field'    => 'text',
  476.                 'Markup Field'  => 'markup',
  477.                 'Int Field'     => 'int',
  478.                 'Double Field'  => 'double',
  479.                 'Bool Field'    => 'bool',
  480.                 'Scalar Field'  => 'scalar',
  481.                 'Pixbuf Field'  => 'pixbuf',
  482.               );
  483.  
  484.   @{$slist->{data}} = (
  485.           [ 'text', 1, 1.1,  TRUE, $var, $pixbuf ],
  486.           [ 'text', 2, 2.2, FALSE, $var, $pixbuf ],
  487.   );
  488.  
  489.   # (almost) anything you can do to an array you can do to 
  490.   # $slist->{data} which is an array reference tied to the list model
  491.   push @{$slist->{data}}, [ 'text', 3, 3.3, TRUE, $var, $pixbuf ];
  492.  
  493.   # mess with selections
  494.   $slist->get_selection->set_mode ('multiple');
  495.   $slist->get_selection->unselect_all;
  496.   $slist->select (1, 3, 5..9); # select rows by index
  497.   $slist->unselect (3, 8); # unselect rows by index
  498.   @sel = $slist->get_selected_indices;
  499.  
  500.   # simple way to make text columns editable
  501.   $slist->set_column_editable ($col_num, TRUE);
  502.  
  503.   # Gtk2::SimpleList derives from Gtk2::TreeView, so all methods
  504.   # on a treeview are available.
  505.   $slist->set_rules_hint (TRUE);
  506.   $slist->signal_connect (row_activated => sub {
  507.           my ($sl, $path, $column) = @_;
  508.       my $row_ref = $sl->get_row_data_from_path ($path);
  509.       # $row_ref is now an array ref to the double-clicked row's data.
  510.       });
  511.  
  512.   # turn an existing TreeView into a SimpleList; useful for
  513.   # Glade-generated interfaces.
  514.   $simplelist = Gtk2::SimpleList->new_from_treeview (
  515.                     $glade->get_widget ('treeview'),
  516.                     'Text Field'    => 'text',
  517.                     'Int Field'     => 'int',
  518.                     'Double Field'  => 'double',
  519.                  );
  520.  
  521. =head1 ABSTRACT
  522.  
  523. SimpleList is a simple interface to the powerful but complex Gtk2::TreeView
  524. and Gtk2::ListStore combination, implementing using tied arrays to make
  525. thing simple and easy.
  526.  
  527. =head1 DESCRIPTION
  528.  
  529. Gtk2 has a powerful, but complex MVC (Model, View, Controller) system used to
  530. implement list and tree widgets.  Gtk2::SimpleList automates the complex setup
  531. work and allows you to treat the list model as a more natural list of lists
  532. structure.
  533.  
  534. After creating a new Gtk2::SimpleList object with the desired columns you may
  535. set the list data with a simple Perl array assignment. Rows may be added or
  536. deleted with all of the normal array operations. You can treat the C<data>
  537. member of the list simplelist object as an array reference, and manipulate the
  538. list data with perl's normal array operators.
  539.  
  540. A mechanism has also been put into place allowing columns to be Perl scalars.
  541. The scalar is converted to text through Perl's normal mechanisms and then
  542. displayed in the list. This same mechanism can be expanded by defining
  543. arbitrary new column types before calling the new function. 
  544.  
  545. =head1 OBJECT HIERARCHY
  546.  
  547.  Glib::Object
  548.  +--- Gtk2::Object
  549.       +--- Gtk2::Widget
  550.            +--- Gtk2::TreeView
  551.             +--- Gtk2::SimpleList
  552.  
  553. =head1 METHODS
  554.  
  555. =over
  556.  
  557. =item $slist = Gtk2::SimpleList->new ($cname, $ctype, ...)
  558.  
  559. =over
  560.  
  561. =over
  562.  
  563. =item * $cname (string)
  564.  
  565. =item * $ctype (string)
  566.  
  567. =back
  568.  
  569. =back
  570.  
  571. Creates a new Gtk2::SimpleList object with the specified columns. The parameter
  572. C<cname> is the name of the column, what will be displayed in the list headers if
  573. they are turned on. The parameter ctype is the type of the column, one of:
  574.  
  575.  text    normal text strings
  576.  markup  pango markup strings
  577.  int     integer values
  578.  double  double-precision floating point values
  579.  bool    boolean values, displayed as toggle-able checkboxes
  580.  scalar  a perl scalar, displayed as a text string by default
  581.  pixbuf  a Gtk2::Gdk::Pixbuf
  582.  
  583. or the name of a custom type you add with C<add_column_type>.
  584. These should be provided in pairs according to the desired columns for your
  585. list.
  586.  
  587. =item $slist = Gtk2::SimpleList->new_from_treeview ($treeview, $cname, $ctype, ...)
  588.  
  589. =over
  590.  
  591. =over
  592.  
  593. =item * $treeview (Gtk2::TreeView)
  594.  
  595. =item * $cname (string)
  596.  
  597. =item * $ctype (string)
  598.  
  599. =back
  600.  
  601. =back
  602.  
  603. Like C<< Gtk2::SimpleList->new() >>, but turns an existing Gtk2::TreeView into
  604. a Gtk2::SimpleList.  This is intended mostly for use with stuff like Glade,
  605. where the widget is created for you.  This will create and attach a new model
  606. and remove any existing columns from I<treeview>.  Returns I<treeview>,
  607. re-blessed as a Gtk2::SimpleList.
  608.  
  609. =item $slist->set_data_array ($arrayref)
  610.  
  611. =over
  612.  
  613. =over
  614.  
  615. =item * $arrayref (array reference)
  616.  
  617. =back
  618.  
  619. =back
  620.  
  621. Set the data in the list to the array reference $arrayref. This is completely
  622. equivalent to @{$list->{data}} = @{$arrayref} and is only here for convenience
  623. and for those programmers who don't like to type-cast and have static, set once
  624. data.
  625.  
  626. =item @indices = $slist->get_selected_indices
  627.  
  628. Return the indices of the selected rows in the ListStore.
  629.  
  630. =item $slist->get_row_data_from_path ($path)
  631.  
  632. =over
  633.  
  634. =over
  635.  
  636. =item * $path (Gtk2::TreePath) the path of the desired row 
  637.  
  638. =back
  639.  
  640. =back
  641.  
  642. Returns an array ref with the data of the row indicated by $path.
  643.  
  644. =item $slist->select ($index, ...);
  645.  
  646. =item $slist->unselect ($index, ...);
  647.  
  648. =over
  649.  
  650. =over
  651.  
  652. =item * $index (integer)
  653.  
  654. =back
  655.  
  656. =back
  657.  
  658. Select or unselect rows in the list by index.  If the list is set for multiple
  659. selection, all indices in the list will be set/unset; otherwise, just the
  660. first is used.  If the list is set for no selection, then nothing happens.
  661.  
  662. To set the selection mode, or to select all or none of the rows, use the normal
  663. TreeView/TreeSelection stuff, e.g.  $slist->get_selection and the TreeSelection
  664. methods C<get_mode>, C<set_mode>, C<select_all>, and C<unselect_all>.
  665.  
  666. =item $slist->set_column_editable ($index, $editable)
  667.  
  668. =over
  669.  
  670. =over
  671.  
  672. =item * $index (integer)
  673.  
  674. =item * $editable (boolean)
  675.  
  676. =back
  677.  
  678. =back
  679.  
  680. =item boolean = $slist->get_column_editable ($index)
  681.  
  682. =over
  683.  
  684. =over
  685.  
  686. =item * $index (integer)
  687.  
  688. =back
  689.  
  690. =back
  691.  
  692. This is a very simple interface to Gtk2::TreeView's editable text column cells.
  693. All columns which use the attr "text" (basically, any text or number column,
  694. see C<add_column_type>) automatically have callbacks installed to update data
  695. when cells are edited.  With C<set_column_editable>, you can enable the
  696. in-place editing.
  697.  
  698. C<get_column_editable> tells you if column I<index> is currently editable.
  699.  
  700. =item Gtk2::SimpleList->add_column_type ($type_name, ...)
  701.  
  702.  
  703. =over
  704.  
  705. =over
  706.  
  707. =item $type_name (string)
  708.  
  709. =back
  710.  
  711. =back
  712.  
  713. Add a new column type to the list of possible types. Initially six column types
  714. are defined, text, int, double, bool, scalar, and pixbuf. The bool column type
  715. uses a toggle cell renderer, the pixbuf uses a pixbuf cell renderer, and the
  716. rest use text cell renderers. In the process of adding a new column type you
  717. may use any cell renderer you wish. 
  718.  
  719. The first parameter is the column type name, the list of six are examples.
  720. There are no restrictions on the names and you may even overwrite the existing
  721. ones should you choose to do so. The remaining parameters are the type
  722. definition consisting of key value pairs. There are three required: type,
  723. renderer, and attr. The type key determines what actual datatype will be
  724. stored in the underlying model representation; this is a package name, e.g.
  725. Glib::String, Glib::Int, Glib::Boolean, but in general if you want an
  726. arbitrary Perl data structure you will want to use 'Glib::Scalar'. The
  727. renderer key should hold the class name of the cell renderer to create for this
  728. column type; this may be any of Gtk2::CellRendererText,
  729. Gtk2::CellRendererToggle, Gtk2::CellRendererPixbuf, or some other, possibly
  730. custom, cell renderer class.  The attr key is magical; it may be either a
  731. string, in which case it specifies the attribute which will be set from the
  732. specified column (e.g. 'text' for a text renderer, 'active' for a toggle
  733. renderer, etc), or it may be a reference to a subroutine which will be called
  734. each time the renderer needs to draw the data.
  735.  
  736. This function, described as a GtkTreeCellDataFunc in the API reference, 
  737. will receive 5 parameters: $treecol, $cell, $model, $iter,
  738. $col_num (when SimpleList hooks up the function, it sets the column number to
  739. be passed as the user data).  The data value for the particular cell in question
  740. is available via $model->get ($iter, $col_num); you can then do whatever it is
  741. you have to do to render the cell the way you want.  Here are some examples:
  742.  
  743.   # just displays the value in a scalar as 
  744.   # Perl would convert it to a string
  745.   Gtk2::SimpleList->add_column_type( 'a_scalar', 
  746.           type     => 'Glib::Scalar',
  747.       renderer => 'Gtk2::CellRendererText',
  748.           attr     => sub {
  749.                my ($treecol, $cell, $model, $iter, $col_num) = @_;
  750.                my $info = $model->get ($iter, $col_num);
  751.                $cell->set (text => $info);
  752.       }
  753.      );
  754.  
  755.   # sums up the values in an array ref and displays 
  756.   # that in a text renderer
  757.   Gtk2::SimpleList->add_column_type( 'sum_of_array', 
  758.           type     => 'Glib::Scalar',
  759.       renderer => 'Gtk2::CellRendererText',
  760.           attr     => sub {
  761.                my ($treecol, $cell, $model, $iter, $col_num) = @_;
  762.                my $sum = 0;
  763.                my $info = $model->get ($iter, $col_num);
  764.                foreach (@$info)
  765.                {
  766.                    $sum += $_;
  767.                }
  768.                $cell->set (text => $sum);
  769.           } 
  770.      );
  771.  
  772. =back
  773.  
  774. =head1 MODIFYING LIST DATA
  775.  
  776. After creating a new Gtk2::SimpleList object there will be a member called C<data>
  777. which is a tied array. That means data may be treated as an array, but in
  778. reality the data resides in something else. There is no need to understand the
  779. details of this it just means that you put data into, take data out of, and
  780. modify it just like any other array. This includes using array operations like
  781. push, pop, unshift, and shift. For those of you very familiar with perl this
  782. section will prove redundant, but just in case:
  783.  
  784.   Adding and removing rows:
  785.   
  786.     # push a row onto the end of the list
  787.     push @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
  788.     # pop a row off of the end of the list
  789.     $rowref = pop @{$slist->{data}};
  790.     # unshift a row onto the beginning of the list
  791.     unshift @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
  792.     # shift a row off of the beginning of the list
  793.     $rowref = shift @{$slist->{data}};
  794.     # delete the row at index $n, 0 indexed
  795.     splice @{ $slist->{data} }, $n, 1;
  796.     # set the entire list to be the data in a array
  797.     @{$slist->{data}} = ( [row1, ...], [row2, ...], [row3, ...] );
  798.  
  799.   Getting at the data in the list:
  800.   
  801.     # get an array reference to the entire nth row
  802.     $rowref = $slist->{data}[n];
  803.     # get the scalar in the mth column of the nth row, 0 indexed
  804.     $val = $slist->{data}[n][m];
  805.     # set an array reference to the entire nth row
  806.     $slist->{data}[n] = [col1_data, col2_data, ..., coln_data];
  807.     # get the scalar in the mth column of the nth row, 0 indexed
  808.     $slist->{data}[n][m] = $rowm_coln_value;
  809.  
  810. =head1 SEE ALSO
  811.  
  812. Perl(1), Glib(3pm), Gtk2(3pm), Gtk2::TreeView(3pm), Gtk2::TreeModel(3pm),
  813. Gtk2::ListStore(3pm).
  814.  
  815. Note: Gtk2::SimpleList is deprecated in favor of Gtk2::Ex::Simple::List, part
  816. of the Gtk2-Perl-Ex project at http://gtk2-perl-ex.sf.net .
  817.  
  818. =head1 AUTHORS
  819.  
  820.  muppet <scott at asofyet dot org>
  821.  Ross McFarland <rwmcfa1 at neces dot com>
  822.  Gavin Brown <gavin dot brown at uk dot com>
  823.  
  824. =head1 COPYRIGHT AND LICENSE
  825.  
  826. Copyright 2003-2004 by the Gtk2-Perl team.
  827.  
  828. This library is free software; you can redistribute it and/or modify it under
  829. the terms of the GNU Library General Public License as published by the Free
  830. Software Foundation; either version 2.1 of the License, or (at your option) any
  831. later version.
  832.  
  833. This library is distributed in the hope that it will be useful, but WITHOUT ANY
  834. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  835. PARTICULAR PURPOSE.  See the GNU Library General Public License for more
  836. details.
  837.  
  838. You should have received a copy of the GNU Library General Public License along
  839. with this library; if not, write to the Free Software Foundation, Inc., 59
  840. Temple Place - Suite 330, Boston, MA  02111-1307  USA.
  841.  
  842. =cut
  843.